package com.miyake.client.datamanage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;

public class ShowImage {
	private DataServiceAsync service = DataService.Util.getInstance();
	
	public ShowImage(String key, Panel panel, String width, String height) {
		showImage(key, panel, width, height);
	}
	
	public ShowImage(NodeProperty node, Panel panel, String width, String height) {
		showImage(node.getKey(), panel, width, height);
	}

	private void showImage(String key, Panel panel, String width, String height) {
		//final HTML image = new HTML();
		final Image image = new Image();
		if (height != null) {
			image.setHeight(height);
		}
		if (width != null) {
			image.setWidth(width);
		}
		panel.add(image);
		service.getImage(key, new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				String url = GWT.getHostPageBaseURL()+result;
				image.setUrl(result);
				//image.setHTML("<div align=\"center\"><img src=\"" + url + "\"></div>");
			}
		});
	}
}
